/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package newpeer;

import ejercicios.HelloWorld;
import java.io.File;
import java.io.IOException;
import java.net.InetAddress;
import java.net.UnknownHostException;
import net.jxta.discovery.DiscoveryService;
import net.jxta.id.ID;
import net.jxta.peer.PeerID;
import net.jxta.peergroup.PeerGroupID;

import java.util.logging.Level;
import java.util.logging.Logger;

import net.jxta.document.MimeMediaType;
import net.jxta.pipe.PipeID;
import net.jxta.pipe.PipeService;
import org.farng.mp3.MP3File;
import org.farng.mp3.TagException;
import org.farng.mp3.id3.ID3v1;

/**
 *
 * @author Manuel
 */
class ThreadPublicacionArchivosCompartidos extends Thread {

    private DiscoveryService discovery;
    private String peerName;

    public ThreadPublicacionArchivosCompartidos(DiscoveryService discovery, String peerName) {
        this.discovery = discovery;
        this.peerName = peerName;
    }

    @Override
    public void run() {

        long lifetime = 60 * 2 * 1000L;
        long expiration = 60 * 2 * 1000L;
        long waittime = 60 * 3 * 1000L;
        while (true) {
            try {
                File rutaCompartidos = new File("../temp/");
                File[] listaArchivos = rutaCompartidos.listFiles();
                for (int i = 0; i < listaArchivos.length; i++) {
                    final File file = listaArchivos[i];
                    if (!file.isDirectory()) {
                        PipeAdvertisementArchivo advTutorial = new PipeAdvertisementArchivo();
                        IdAssigment idassigne = new IdAssigment();
                        PeerGroupID advgroup = IdAssigment.createPeerGroupID("Nuevo Group");
                        // Desde la interfaz se le asigna un nombre al peer:
                        PeerID advPeerId = IdAssigment.createPeerID(advgroup, file.getName());
                        PipeID advPipeId = IdAssigment.createNewPipeID(advgroup);
                        advTutorial.setType(PipeService.UnicastType);
                        advTutorial.setID(advPeerId);
                       /* advTutorial.setID(new ID() {

                            @Override
                            public String getIDFormat() {
                                return file.getName();
                            }

                            @Override
                            public Object getUniqueValue() {
                                return file.getName();
                            }
                        });*/
                        ID Laidpeer = advTutorial.getID();
                        advTutorial.setName("Archivo X");
                        try {
                            advTutorial.setIP(InetAddress.getLocalHost().getHostAddress());
                        } catch (UnknownHostException ignored) {
                        }
                        advTutorial.setNombreArchivo(file.getName());
                        advTutorial.setTamanoArchivo(Long.toString(file.length()));

                        advTutorial.setOSArch(System.getProperty("os.arch"));
                        advTutorial.setHWArch(System.getProperty("HOSTTYPE",
                                System.getProperty("os.arch")));
                        advTutorial.setHWVendor(System.getProperty("java.vm.vendor"));
                        if (file.getName().endsWith(".mp3")) {
                            try {
                                MP3File archivoMp3 = new MP3File(file);
                                ID3v1 tagInfo = archivoMp3.getID3v1Tag();
                                if (tagInfo != null) {
                                    String albumCancion = tagInfo.getAlbumTitle();
                                    String artistaCancion = tagInfo.getArtist();
                                    String nombreCancion = tagInfo.getSongTitle();
                                    String anoCancion = tagInfo.getYear();
                                    advTutorial.setAno(anoCancion);
                                    advTutorial.setNombreAlbum(albumCancion);
                                    advTutorial.setNombreArtista(artistaCancion);
                                    advTutorial.setNombreCancion(nombreCancion);
                                }
                            //System.out.println("INFO: " + albumCancion + "\n" + artistaCancion + "\n" + nombreCancion + "\n" + anoCancion + "\n");
                            } catch (IOException ex) {
                                Logger.getLogger(HelloWorld.class.getName()).log(Level.SEVERE, null, ex);
                            } catch (TagException ex) {
                                Logger.getLogger(HelloWorld.class.getName()).log(Level.SEVERE, null, ex);
                            }
                        }
                        System.out.println(
                                "Publicando advertisements :" + lifetime + " expiracion :" + expiration + "La ID del peer: " + Laidpeer);
                        System.out.println(advTutorial.getDocument(MimeMediaType.XMLUTF8).toString());
                        discovery.publish(advTutorial, lifetime, expiration);
                        //discovery.remotePublish(advTutorial, expiration);
                    }
                }
                try {
                    System.out.println("Sleeping for :" + waittime);
                    Thread.sleep(waittime);
                } catch (Exception e) {// ignored
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
}
